﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using BLL.BatchBLL;
using DAL;
using Model;

namespace MDMUI
{
    public partial class unCrteBatch : Form
    {
        private unCrteBatchBLL batchService = new unCrteBatchBLL(); // 创建批次服务实例
        private BatchRepository bacthData = new BatchRepository(); // 创建批次服务实例
        private List<Batch> unCrteBatches = new List<Batch>(); // 用于存储批次数据的列表

        public unCrteBatch()
        {
            InitializeComponent(); // 初始化窗体组件
            InitializeDataGridView(); // 初始化DataGridView组件
        }

        private void InitializeDataGridView()
        {
            // 清空现有列
            dataGridView1.Columns.Clear();

            // 设置 DataGridView 的属性
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.ReadOnly = false;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; // 设置选择模式为整行选择

            // 添加复选框列
            DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
            checkBoxColumn.ReadOnly = false; // 设置复选框列为可编辑
            checkBoxColumn.HeaderText = "";
            checkBoxColumn.Name = "Select";
            checkBoxColumn.Width = 30;

            dataGridView1.Columns.Add(checkBoxColumn);

            // 添加列
            DataGridViewTextBoxColumn colBatchId = new DataGridViewTextBoxColumn();
            colBatchId.Name = "BatchId";
            colBatchId.ReadOnly = true;
            colBatchId.HeaderText = "批次号";
            colBatchId.DataPropertyName = "BatchId";
            colBatchId.Width = 80;
            dataGridView1.Columns.Add(colBatchId);

            DataGridViewTextBoxColumn colProductId = new DataGridViewTextBoxColumn();
            colProductId.Name = "ProductId";
            colProductId.HeaderText = "产品号";
            colProductId.DataPropertyName = "ProductId";
            colProductId.Width = 80;
            colProductId.ReadOnly = true;
            dataGridView1.Columns.Add(colProductId);

            DataGridViewTextBoxColumn colWorkOrderId = new DataGridViewTextBoxColumn();
            colWorkOrderId.Name = "WorkOrderId";
            colWorkOrderId.HeaderText = "工单号";
            colWorkOrderId.DataPropertyName = "WorkOrderId";
            colWorkOrderId.Width = 150;
            colWorkOrderId.ReadOnly = true;
            dataGridView1.Columns.Add(colWorkOrderId);

            DataGridViewTextBoxColumn colTotalProcessId = new DataGridViewTextBoxColumn();
            colTotalProcessId.Name = "TotalProcessId";
            colTotalProcessId.HeaderText = "工艺流程号";
            colTotalProcessId.DataPropertyName = "TotalProcessId";
            colTotalProcessId.ReadOnly = true;
            dataGridView1.Columns.Add(colTotalProcessId);

            DataGridViewTextBoxColumn colBatchType = new DataGridViewTextBoxColumn();
            colBatchType.Name = "BatchType";
            colBatchType.HeaderText = "批次类型";
            colBatchType.DataPropertyName = "BatchType";
            colBatchType.Width = 100;
            colBatchType.ReadOnly = true;
            dataGridView1.Columns.Add(colBatchType);

            DataGridViewTextBoxColumn colQuantity = new DataGridViewTextBoxColumn();
            colQuantity.Name = "Quantity";
            colQuantity.HeaderText = "批次数量";
            colQuantity.DataPropertyName = "Quantity";
            colQuantity.Width = 80;
            colBatchType.ReadOnly = true;
            dataGridView1.Columns.Add(colQuantity);

            DataGridViewTextBoxColumn colSubQuantity = new DataGridViewTextBoxColumn();
            colSubQuantity.Name = "SubQuantity";
            colSubQuantity.HeaderText = "子产品数量";
            colSubQuantity.DataPropertyName = "SubQuantity";
            colSubQuantity.Width = 80;
            colSubQuantity.ReadOnly = true;
            dataGridView1.Columns.Add(colSubQuantity);

            DataGridViewTextBoxColumn colGoodQuantity = new DataGridViewTextBoxColumn();
            colGoodQuantity.Name = "GoodQuantity";
            colGoodQuantity.HeaderText = "Good数量";
            colGoodQuantity.DataPropertyName = "GoodQuantity";
            colGoodQuantity.Width = 80;
            colSubQuantity.ReadOnly = true;
            dataGridView1.Columns.Add(colGoodQuantity);

            DataGridViewTextBoxColumn colNGQuantity = new DataGridViewTextBoxColumn();
            colNGQuantity.Name = "NGQuantity";
            colNGQuantity.HeaderText = "NG数量";
            colNGQuantity.DataPropertyName = "NGQuantity";
            colNGQuantity.Width = 80;
            colNGQuantity.ReadOnly = true;
            dataGridView1.Columns.Add(colNGQuantity);

            DataGridViewTextBoxColumn colUnit = new DataGridViewTextBoxColumn();
            colUnit.Name = "Unit";
            colUnit.HeaderText = "单位";
            colUnit.DataPropertyName = "Unit";
            colUnit.Width = 80;
            colUnit.ReadOnly = true;
            dataGridView1.Columns.Add(colUnit);

            DataGridViewTextBoxColumn colLockStatus = new DataGridViewTextBoxColumn();
            colLockStatus.Name = "LockStatus";
            colLockStatus.HeaderText = "投产状态";
            colLockStatus.DataPropertyName = "LockStatus";
            colLockStatus.Width = 80;
            colLockStatus.ReadOnly = true;
            dataGridView1.Columns.Add(colLockStatus);

            DataGridViewTextBoxColumn colEventName = new DataGridViewTextBoxColumn();
            colEventName.Name = "EventName";
            colEventName.HeaderText = "事件名称";
            colEventName.DataPropertyName = "EventName";
            colEventName.Width = 120;
            colEventName.ReadOnly = true;
            dataGridView1.Columns.Add(colEventName);

            DataGridViewTextBoxColumn colEventUser = new DataGridViewTextBoxColumn();
            colEventUser.Name = "EventUser";
            colEventUser.HeaderText = "事件用户";
            colEventUser.DataPropertyName = "EventUser";
            colEventUser.Width = 120;
            colEventUser.ReadOnly = true;
            dataGridView1.Columns.Add(colEventUser);

            DataGridViewTextBoxColumn colEventTime = new DataGridViewTextBoxColumn();
            colEventTime.Name = "EventTime";
            colEventTime.HeaderText = "创建时间";
            colEventTime.DataPropertyName = "EventTime";
            colEventTime.Width = 120;
            colEventTime.ReadOnly = true;
            dataGridView1.Columns.Add(colEventTime);

            DataGridViewTextBoxColumn colEventRemarks = new DataGridViewTextBoxColumn();
            colEventRemarks.Name = "EventRemarks";
            colEventRemarks.HeaderText = "事件备注";
            colEventRemarks.DataPropertyName = "EventRemarks";
            colEventRemarks.Width = 200;
            colEventRemarks.ReadOnly = true;
            dataGridView1.Columns.Add(colEventRemarks);

            // 调整列的自动填充模式
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        }

        // 查询按钮点击事件
        private void button1_Click(object sender, EventArgs e)
        {
            PerformBatchSearch(sender, e);
        }

        // 执行批次搜索
        private void PerformBatchSearch(object sender, EventArgs e)
        {
            // 获取输入的批次号
            string batchIdStr = textBox1.Text.Trim();

            // 获取日期范围
            //DateTime startDate = DateTime.MinValue;
            //DateTime endDate = DateTime.MaxValue;

            if (string.IsNullOrEmpty(batchIdStr))
            {
                // 如果没有输入批次号，获取所有未投产的批次
                unCrteBatches = batchService.GetAllBatches().FindAll(b => b.LockStatus != "已投产");
            }
            else
            {
                int batchId;
                if (!int.TryParse(batchIdStr, out batchId))
                {
                    MessageBox.Show("输入的批次号必须是整数！");
                    return;
                }

                // 根据批次号筛选未投产的批次
                unCrteBatches = batchService.GetAllBatches().FindAll(b => b.BatchId == batchId && b.LockStatus != "已投产");
                if (unCrteBatches.Count == 0)
                {
                    MessageBox.Show("未找到指定的批次，请检查输入的批次号是否正确！");
                }
            }

            // 绑定数据到 DataGridView
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = unCrteBatches;
        }

        // 处理复选框点击事件
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns["Select"].Index && e.RowIndex >= 0)
            {
                bool currentValue = false;
                if (dataGridView1.CurrentCell.Value != null && dataGridView1.CurrentCell.Value != DBNull.Value)
                {
                    currentValue = (bool)dataGridView1.CurrentCell.Value;
                }
                dataGridView1.CurrentCell.Value = !currentValue;
            }
        }

        private void unCrteBatch_Load(object sender, EventArgs e)
        {
            // 设备表
            BatchRepository batchRepository = new BatchRepository();
            List<Batch> batchData = batchRepository.GetAllBatches();
            try
            {
                // 清空 ComboBox
                comboBox1.Items.Clear();
                // 遍历设备数据并添加到 ComboBox
                foreach (var dateTime in batchData)
                {
                    comboBox1.Items.Add(dateTime.EventTime);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error loading equipment models: {ex.Message}");
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource != null && unCrteBatches.Count > 0)
            {
                if (comboBox1.SelectedItem != null)
                {
                    DateTime selectedDate = (DateTime)comboBox1.SelectedItem;

                    // 筛选与选中日期相匹配的批次且未投产
                    List<Batch> filteredBatches = unCrteBatches
                        .FindAll(b => b.EventTime.Date == selectedDate.Date && b.LockStatus != "已投产");

                    // 绑定筛选后的数据到 DataGridView
                    dataGridView1.DataSource = null;
                    dataGridView1.DataSource = filteredBatches;
                }
            }
            else
            {
                MessageBox.Show("请先查询数据！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 删除选中行
            if (dataGridView1.SelectedRows.Count > 0)
            {
                List<int> selectedBatchIds = new List<int>();
                foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                {
                    int batchId = (int)row.Cells["BatchId"].Value;
                    selectedBatchIds.Add(batchId);
                }

                foreach (int batchId in selectedBatchIds)
                {
                    batchService.DeleteBatch(batchId); // 删除批次
                }

                // 重新加载数据
                PerformBatchSearch(sender, e);
            }
            else
            {
                MessageBox.Show("请先选择要删除的批次！");
            }
        }
    }
}